package org.windowkit.toolkit.frame;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;



/**
 * The Glass Pane used when a there is DB work being done. Traps user input
 * events and overlays a message box indicating that work is being done.
 *
 * @author oconnellr
 *
 */
public class BusyGlassPane extends GlassPane {

	/**
	 *
	 */
	private static final long serialVersionUID = 5201015555143071076L;

	protected final static String BUSY_MESSAGE = "Working...";

	JButton cmdCancel = new JButton("Cancel");

	JProgressBar progressBar = new JProgressBar();

	JLabel lblBusy = new JLabel(BUSY_MESSAGE);

	Box myBox;

	/**
	 *
	 */
	public BusyGlassPane() {
		super();
		progressBar.setIndeterminate(true);
		setCancelVisible(false);
		layoutGlassPane();
	}

	/*
	 * (non-Javadoc)
	 */
	protected void layoutGlassPane() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		myBox = new Box(BoxLayout.Y_AXIS);

		JPanel p = new JPanel(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();

		gbc.insets = new Insets(5, 5, 5, 5);

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.gridwidth = 3;

		p.add(lblBusy, gbc);

		gbc.gridx = 1;
		gbc.gridy = 1;
		gbc.gridwidth = 1;

		gbc.fill = GridBagConstraints.HORIZONTAL;
		p.add(progressBar, gbc);

		gbc.gridx = 1;
		gbc.gridy = 2;
		gbc.fill = GridBagConstraints.NONE;
		cmdCancel.setPreferredSize(new Dimension(100, cmdCancel
				.getPreferredSize().height));
		p.add(cmdCancel, gbc);

		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.weightx = 1;
		p.add(Box.createHorizontalGlue(), gbc);
		gbc.gridx = 2;
		p.add(Box.createHorizontalGlue(), gbc);

		p.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.RAISED),
				new CompoundBorder(new EmptyBorder(new Insets(12, 12, 12, 12)),
						new EtchedBorder(EtchedBorder.LOWERED))));

		p.setMaximumSize(new Dimension(250, 150));
		myBox.add(p);

		add(Box.createVerticalGlue());
		add(myBox);
		add(Box.createVerticalGlue());
	}

	public void setFocused() {
		// cmdCancel.requestFocusInWindow();
		lblBusy.requestFocusInWindow();
	}

	public void addActionListener(final ActionListener listener) {
		cmdCancel.addActionListener(listener);
	}

    public void clearActionListeners(){
        ActionListener[] listeners = cmdCancel.getActionListeners();
        for(ActionListener list : listeners){
            cmdCancel.removeActionListener(list);
        }
    }

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.swing.AbstractButton#setEnabled(boolean)
	 */
	public void setCancelEnabled(final boolean b) {
		cmdCancel.setEnabled(b);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see javax.swing.JComponent#setVisible(boolean)
	 */
	public void setCancelVisible(final boolean aFlag) {
		cmdCancel.setVisible(aFlag);
	}

}
